//
//  FileTool.swift
//  swiftdemo
//
//  Created by Galaxy on 2020/12/24.
//  Copyright © 2020 liuqingyuan. All rights reserved.
//

import UIKit
import WCDBSwift
class FileTool: NSObject {
    /// 获取数据库位置
        func getDb() -> Database {
            let pathUrl = getDocumentPath()+"/imageData/database.db"
            return Database(at: pathUrl)
        }
    /// 获取document文件夹
    /// - Returns: document位置夹位置
    func getDocumentPath() -> String {
        let documentPath:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        return documentPath
    }
    
    //    创建文件夹
    func createDirectory(path:String) throws ->Bool{
        let fileManger = FileManager.default
        var isDirectory:ObjCBool = false
        let isExist = fileManger.fileExists(atPath: path, isDirectory: &isDirectory)
        // 文件是否存在
        if !isExist {
            do{
                try fileManger.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
                return true
            }catch let error{
                print(error)
                return false
            }
        }else{
            return true
        }
    }
    // 创建文件
//    func createFile(document:String,fileData:Data) -> Bool {
//        let path = self.getDocumentPath().appending(document)
//        let fileManager = FileManager.default
//        let isDirExist = fileManager.fileExists(atPath: path)
//        if !isDirExist {
//            let bCreateDir = fileManager.createFile(atPath: path, contents: fileData, attributes: nil)
//            if bCreateDir {
//                return true
//            }else{
//                return false
//            }
//        }else{
//            return true
//        }
//    }
    
    func createLocalFile(savePath:String,fileName:String,fileData:Data)->Bool{
        let filePath = savePath.appending("/"+fileName)
        let fileManager = FileManager.default
        let isDirExist = fileManager.fileExists(atPath: filePath)
        if !isDirExist {
            let bCreateDir = fileManager.createFile(atPath: filePath, contents: fileData, attributes: nil)
            if bCreateDir {
                return true
            }else{
                return false
            }
        }else{
            return true
        }
    }
    
    // 更新文件
    func updateLocalFile(savePath:String,fileName:String,fileData:Data)->Bool{
        let filePath = savePath.appending("/"+fileName)
        let fileManager = FileManager.default
        let bCreateDir = fileManager.createFile(atPath: filePath, contents: fileData, attributes: nil)
        if bCreateDir {
            return true
        }else{
            return false
        }
    }
    
    /// 删除项目
    /// - Parameter path: 文件位置
    /// - Returns: 删除结果
    func deleteFileWithPath(path:String) -> Bool {
        let fileManager = FileManager.default
        let result:Bool =  ((try? fileManager.removeItem(atPath: path)) != nil)
        return result
    }
}
